<template>

  <div class="login-box">
    <div class="avatar-box">
      <img src="../assets/logo.png" alt="">
    </div>
    <el-form :model="form" :rules="regform" ref="resentRef">
      <el-form-item prop="username">
        <el-input class="login_user login_u " v-model="form.username"  prefix-icon="iconfont icon-user" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input class="login_user " v-model="form.password"  prefix-icon="iconfont icon-lock_fill" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
    </el-form>
    <div class="bun_box">
      <el-button type="primary" @click="login">登录</el-button>
      <el-button type="info" @click="resetFormDate">重置</el-button>
    </div>
  </div>

</template>

<script>
export default {
  data(){
    return {
      form: {
        username: '',
        password: ''
      },
      regform: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  name: 'Login',
  methods: {
    resetFormDate() {
      this.$refs.resentRef.resetFields()
    },
    login() {
      this.$refs.resentRef.validate(async valid => {
        if (!valid) return this.$message.error('登录失败')
        const {data:  res} = await this.$http.post('login',this.form)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token',res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style  scoped>
.login_u {
  margin-top: 100px;
}
.login_user {
  padding: 0 15px 0 0;
  margin-left: 10px;
  margin-right: 10px;
  box-sizing: border-box;
}
.bun_box {
  display: flex;
  justify-content: flex-end;
  margin-right: 20px;
}
.login-box {
  background-color: darkturquoise;
  width: 500px;
  height: 320px;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.avatar-box {
  padding: -10px;
  width: 130px;
  height: 130px;
  border: 1px solid pink;
  border-radius: 100px;
  background-color: ghostwhite;
  position: absolute;
  left: 50%;
  top: 4%;
  transform: translate(-50%,-50%);
}
.avatar-box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: pink;
}
</style>
